import {
	MPickupViewActionType,
	IMPickupViewStore,
	IMPickupViewAbility,
	IMPickupViewController,
	IMPickupViewControllerParams,
	IParam,
	IPickupViewPanelAbility,
} from '@/core/interface';
import { PickupViewController } from './pickup-view-controller';

/**
 * 选择视图控制器
 *
 * @export
 * @class PickupViewController
 * @extends {(DEViewController<T | PickupViewActionType, IPickupViewStore, IPickupViewAbility>)}
 * @template T
 */
export class MPickupViewController
	extends PickupViewController<
		MPickupViewActionType,
		IMPickupViewStore,
		IMPickupViewAbility
	>
	implements IMPickupViewController<IMPickupViewStore, IMPickupViewAbility>
{
	/**
	 * 处理视图初始化
	 *
	 * @protected
	 * @param {IMPickupViewControllerParams<MPickupViewActionType,IMPickupViewAbility>} params
	 * @memberof ExpViewController
	 */
	protected processViewInit(
		params: IMPickupViewControllerParams<
			MPickupViewActionType,
			IMPickupViewAbility
		>
	) {
		super.processViewInit(params);
		Object.assign(this.store, {
			selections: [],
			tempSelections: [],
			panelSelectedKeys: [],
		});
	}

	/**
	 * 按钮行为
	 *
	 * @private
	 * @type {string}
	 * @memberof MPickupViewController
	 */
	private buttonAction = '';

	/**
	 * 是否默认选中
	 *
	 * @private
	 * @type {boolean}
	 * @memberof MPickupViewController
	 */
	private isDefaultSelect = true;

	/**
	 * 处理选择视图按钮行为
	 *
	 * @param {("toLeft"
	 *       | "toRight"
	 *       | "toAllLeft"
	 *       | "toAllRight")} type
	 * @memberof MPickupViewController
	 */
	public handleMPickupViewPickButtonAction(
		type: 'toLeft' | 'toRight' | 'toAllLeft' | 'toAllRight'
	): void {
		this.buttonAction = type;
		switch (type) {
			case 'toLeft':
				this.handleUnSelectItems();
				break;
			case 'toRight':
				this.handleSelectItems();
				break;
			case 'toAllLeft':
				this.handleUnSelectAll();
				break;
			case 'toAllRight':
				this.handleSelectAll();
				break;
		}
	}

	/**
	 * 处理值变更
	 *
	 * @protected
	 * @param {IParam[]} data
	 * @memberof PickupViewController
	 */
	protected handleValueChange(data: IParam[]) {
		this.computeDefaultSelect();
		if (
			this.buttonAction === 'toAllRight' ||
			this.buttonAction === 'toLeft' ||
			this.buttonAction === 'toRight' ||
			this.buttonAction === 'toAllLeft'
		) {
			this.store.selections = data;
		}
		this.buttonAction = '';
		this.store.panelSelectedKeys.length = 0;
		this.store.tempSelections = data;
	}

	/**
	 * 计算默认选中（外部有值时第一次进入页面计算默认选中）
	 *
	 * @private
	 * @param {IParam[]} data
	 * @return {*}
	 * @memberof MPickupViewController
	 */
	private computeDefaultSelect() {
		if (!this.isDefaultSelect) {
			return;
		}
		const { viewParams } = this.store;
		if (
			this.isDefaultSelect &&
			viewParams &&
			viewParams.selectedData.length !== 0
		) {
			this.store.selections = viewParams.selectedData;
		}
		this.isDefaultSelect = false;
	}

	/**
	 * 处理数据激活
	 *
	 * @protected
	 * @param {IParam[]} data
	 * @memberof PickupViewController
	 */
	protected handleDataActive(data: IParam[]) {
		this.emit('viewDataChange', this.store.selections);
		this.emit('viewClose', []);
	}

	/**
	 * 选中数据
	 *
	 * @protected
	 * @memberof MPickupViewController
	 */
	protected handleSelectItems() {
		const ability = this.getPickupViewPanelAbility();
		if (ability) {
			ability.selectItems(this.store.tempSelections);
		}
	}

	/**
	 * 取消选中数据
	 *
	 * @protected
	 * @memberof MPickupViewController
	 */
	protected handleUnSelectItems() {
		const ability = this.getPickupViewPanelAbility();
		if (ability) {
			const items = this.store.selections.filter((item: IParam) => {
				return this.store.panelSelectedKeys.indexOf(item.srfkey) === -1;
			});
			ability.selectItems(items);
		}
	}

	/**
	 * 选中全部
	 *
	 * @protected
	 * @memberof MPickupViewController
	 */
	protected handleSelectAll() {
		const ability = this.getPickupViewPanelAbility();
		if (ability) {
			ability.selectAll();
		}
	}

	/**
	 * 取消选中全部
	 *
	 * @protected
	 * @memberof MPickupViewController
	 */
	protected handleUnSelectAll() {
		const ability = this.getPickupViewPanelAbility();
		if (ability) {
			ability.selectItems([]);
		}
	}

	/**
	 * 处理多数据视图行为
	 *
	 * @param {string} action
	 * @param {IParam} data
	 * @memberof MPickupViewController
	 */
	public handleMPickupViewAction(action: string, data: IParam): void {
		if (action === 'panelItemClick') {
			this.pickerItemClick(data);
		}
	}

	/**
	 * 选择项点击
	 *
	 * @param {IParam} item
	 * @memberof MPickupViewController
	 */
	private pickerItemClick(item: IParam) {
		const index = this.store.panelSelectedKeys.findIndex(
			(key: string) => key === item.srfkey
		);
		if (index === -1) {
			this.store.panelSelectedKeys.push(item.srfkey);
		} else {
			this.store.panelSelectedKeys.slice(index, 1);
		}
	}
}
